﻿using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ArmyCreator.ViewModels
{
    public class ListingViewModel : ALoadable<DataModel.Listing>, INotifyPropertyChanged
    {

        #region FIELDS
        private String _name;

        public String Name
        {
            get { return _name; }
            set { _name = value; NotifyPropertyChanged("Name"); }
        }

        private Int32 _points;

        public Int32 Points
        {
            get { return _points; }
            set { _points = value; NotifyPropertyChanged("Points"); }
        }

        private Int32 _max;

        public Int32 Max
        {
            get { return _max; }
            set { _max = value; NotifyPropertyChanged("Max"); }
        }

        private ArmyViewModel army;

        public ArmyViewModel Army
        {
            get { return army; }
            set { army = value; NotifyPropertyChanged("Name"); }
        }

        #endregion

        public ObservableCollection<CategoryViewModel> Categories { get; set; }

        public ListingViewModel()
        {
            Categories = new ObservableCollection<CategoryViewModel>();
        }

        protected override void Load(DataModel.Listing model, int depth)
        {
            this.Army = ArmyViewModel.LoadData<ArmyViewModel>(model.Army, depth - 1);

            if (model.Categories != null)
            {
                foreach (var category in model.Categories)
                {
                    CategoryViewModel categoryViewModel = new CategoryViewModel();
                    categoryViewModel.LoadData(category, depth - 1);
                    this.Categories.Add(categoryViewModel);
                }
            }
        }

        #region NOTIFICATIONS
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
